package com.thb.pay.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.thb.pay.core.utils.PageResult;
import com.thb.pay.db.service.impl.BaseServiceImpl;
import com.thb.pay.db.utils.PageResultUtils;
import com.thb.pay.db.utils.PageUtils;
import com.thb.pay.db.utils.QueryUtils;
import com.thb.pay.entity.BankCardBin;
import com.thb.pay.mapper.BankCardBinMapper;
import com.thb.pay.service.BankCardBinService;
import com.thb.pay.vo.req.BankCardBinPageReq;
import com.thb.pay.vo.req.BankCardBinReq;
import com.thb.pay.vo.resp.BankCardBinVo;
import com.xiaoleilu.hutool.bean.BeanUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * <p>
 * 银行卡Bin-信息表 服务实现类
 * </p>
 *
 * @author lucky
 * @since 2019-07-21
 */
@Service
public class BankCardBinServiceImpl extends BaseServiceImpl<BankCardBinMapper, BankCardBin> implements BankCardBinService {

    @Override
    public PageResult<BankCardBinVo> findPage(BankCardBinPageReq pageReq) {
        IPage<BankCardBin> currentPage = PageUtils.buildPage(pageReq);
        QueryWrapper wrapper = QueryUtils.buildQueryPage(pageReq);
        IPage<BankCardBin> pageResult = super.page(currentPage, wrapper);
        return PageResultUtils.build(pageResult, BankCardBinVo.class);
    }

    @Override
    public void add(BankCardBinReq bankCardBinReq) {
        Assert.notNull(bankCardBinReq, "bankCardBinReq.not.null");
        BankCardBin bankCardBin = new BankCardBin();
        BeanUtil.copyProperties(bankCardBinReq, bankCardBin);
        super.insert(bankCardBin);
    }

    @Override
    public void addBatch(List<BankCardBinReq> bankCardBinReqList) {
        Assert.notEmpty(bankCardBinReqList, "bankCardBinReq.batch.not.null");
        List<BankCardBin> bankCardBinList = Lists.newArrayList();
        bankCardBinReqList.forEach(bankCardBinReq -> {
            BankCardBin bankCardBin = new BankCardBin();
            BeanUtil.copyProperties(bankCardBinReq, bankCardBin);
            bankCardBinList.add(bankCardBin);
        });
        super.insertBatch(bankCardBinList);
    }

    @Override
    public void update(BankCardBinReq bankCardBinReq) {
        Assert.notNull(bankCardBinReq, "bankCardBinReq.not.null");
        Assert.notNull(bankCardBinReq.getId(), "bankCardBinReq.not.null");
        BankCardBin bankCardBin = super.selectById(bankCardBinReq.getId());
        Assert.notNull(bankCardBin, "bankCardBin.not.null");
        BankCardBin targetBankCardBin = new BankCardBin();
        BeanUtil.copyProperties(bankCardBinReq, targetBankCardBin);
        super.updateById(targetBankCardBin);
    }

    @Override
    public BankCardBinVo getById(Long id) {
        Assert.notNull(id, "bankCardBinId.not.null");
        BankCardBin bankCardBin = super.selectById(id);
        if (bankCardBin != null) {
            BankCardBinVo bankCardBinVo = new BankCardBinVo();
            BeanUtil.copyProperties(bankCardBin, bankCardBinVo);
            return bankCardBinVo;
        }
        return null;
    }

    @Override
    public void deleteById(Long id) {
        BankCardBin bankCardBin = super.selectById(id);
        Assert.notNull(id, "bankCardBinId.not.exist");
        super.deleteById(id);
    }

    @Override
    public void deleteBatchIds(List<Long> ids) {
        Assert.notEmpty(ids, "bankCardBinId.batch.not.null");
        super.deleteBatchIds(ids);
    }
}
